package com.itheima.sc.web.ums;

import com.itheima.sc.dto.RoleUpdateDto;
import com.itheima.sc.dto.UpdatePasswordDto;
import com.itheima.sc.pojo.ums.Admin;
import com.itheima.sc.pojo.ums.UmsRole;
import com.itheima.sc.result.Result;
import com.itheima.sc.service.ums.AdminService;
import com.itheima.sc.vo.AdminInfoVo;
import com.itheima.sc.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> param) {
        Map map = adminService.login(param);
        return Result.success(map);
    }

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/info")
    public Result AdminInfo() {
        AdminInfoVo adminInfoVo = adminService.adminInfo();
        return Result.success(adminInfoVo);
    }

    /**
     * 根据用户名或姓名分页获取用户列表
     */
    @GetMapping("list")
    public Result list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, String keyword) {
        PageResult pageResult = adminService.list(pageNum, pageSize, keyword);
        return Result.success(pageResult);
    }

    /**
     * 登出
     */
    @PostMapping("logout")
    public Result logout() {
        adminService.logout();
        return Result.success(null);
    }

    /**
     * 刷新token
     */
    @GetMapping("refreshToken")
    public Result refreshToken() {
        Map<String,Object> map = adminService.refreshToken();
        return Result.success(map);
    }

    /**
     * 用户注册
     */
    @PostMapping("register")
    public Result register(@RequestBody Admin admin) {
        Admin admin1 = adminService.register(admin);
        return Result.success(admin1);
    }

    /**
     * 给用户分配角色
     */
    @PostMapping("/role/update")
    public Result roleUpdate( Integer[] roleIds,Integer adminId) {
        adminService.roleUpdate(roleIds, adminId);
        return Result.success(null);
    }

    /**
     * 获取指定用户的角色
     */
    @GetMapping("role/{adminId}")
    public Result specificRole(@PathVariable Integer adminId) {
        List<UmsRole> list = adminService.specificRole(adminId);
        return Result.success(list);
    }

    /**
     * 修改指定用户信息
     */
    @PostMapping("update/{id}")
    public Result update(@RequestBody Admin admin) {
        adminService.update(admin);
        return Result.success(null);
    }

    /**
     * 修改指定用户密码
     */
    @PostMapping("updatePassword")
    public Result updatePassword(@RequestBody UpdatePasswordDto updatePasswordDto) {
        adminService.updatePassword(updatePasswordDto);
        return Result.success(null);
    }

    /**
     * 修改账号状态
     */
    @PostMapping("updateStatus/{id}")
    public Result updateStatus(@PathVariable Integer id, Integer status) {
        adminService.updateStatus(id, status);
        return Result.success(null);
    }

    /**
     * 获取指定用户信息
     */
    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id) {
        Admin admin = adminService.getById(id);
        return Result.success(admin);
    }

    /**
     * 删除指定用户
     */
    @PostMapping("delete/{id}")
    private Result delete(@PathVariable Integer id){
        adminService.delete(id);
        return Result.success(null);
    }
}
